//
//  AppDelegate.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/18.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // 修复1.0的Bug
    // 2.0准备上线, 发现Bug,一招致命
    // 完成2.0Bug修改,准备2.1发布
    // 改掉了Bug
    // 改掉了Bug
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        SQLiteManager.sharedManager
        
        // 设置导航栏的皮肤
        setupAppearance()
        
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        
        let userAccount = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount
        print("userAccount: \(userAccount)")
        
        // 创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        let tabBarVC = HMMainTabBarController()
        tabBarVC.view.backgroundColor = UIColor.whiteColor()
        
        // 设置根控制器
//        window?.rootViewController = tabBarVC
        
//        window?.rootViewController = HMWelcomeViewController()
        
//        window?.rootViewController = HMNewFeatureViewController()
        window?.rootViewController = defaultViewController()
        
        // 窗口显示
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /// 类方法给外面的人调用来切换界面
    class func switchRootViewController(controller: UIViewController) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = controller
    }
    
    /// 进入程序根据条件直接进入某个控制器
    private func defaultViewController() -> UIViewController {
        // 判断是否登录
        if !HMUserAccountViewModel.sharedUserAccountViewModel.userLogin {
            // 没有登录
            return HMMainTabBarController()
        } else {
            // 登录了
            let newVersion = isNewVersion()
            return newVersion ? HMNewFeatureViewController() : HMWelcomeViewController()
        }
    }
    
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取程序当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 获取上次运行的版本
        let key = "lastVersion"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(key)
        
        let isNewVersion = currentVersion != lastVersion
        // 比较
        if isNewVersion {
            // 新版本
            // 保存新版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: key)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 返回
        return isNewVersion
    }
    
    /// 设置皮肤
    private func setupAppearance() {
        let naviBar = UINavigationBar.appearance()
        naviBar.tintColor = UIColor.orangeColor()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        HMStatusDAL.sharedStatusDAL.clearCacheStatus()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

